/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reportcenter.auth.ui;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import org.springframework.binding.value.ValueModel;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.richclient.application.PageComponentContext;
import org.springframework.richclient.application.event.LifecycleApplicationEvent;
import org.springframework.richclient.application.support.AbstractView;
import org.springframework.richclient.command.ActionCommand;
import org.springframework.richclient.command.ActionCommandExecutor;
import org.springframework.richclient.command.CommandGroup;
import org.springframework.richclient.command.GuardedActionCommandExecutor;
import org.springframework.richclient.command.support.AbstractActionCommandExecutor;
import org.springframework.richclient.command.support.GlobalCommandIds;
import org.springframework.richclient.dialog.ConfirmationDialog;
import org.springframework.richclient.list.ListSelectionValueModelAdapter;
import org.springframework.richclient.list.ListSingleSelectionGuard;
import reportcenter.auth.business.RolesDao;
import reportcenter.auth.business.UserAuthorizationDao;
import reportcenter.auth.dominio.Users;

/**
 *
 * @author renan
 */

public class UserAuthView extends AbstractView implements ApplicationListener{
    
    private UserAuthTable userAuthTable;
    private UserAuthorizationDao userAuthDao;
    private RolesDao roleDao;

    public UserAuthTable getUserAuthTable() {
        return userAuthTable;
    }

    public void setUserAuthTable(UserAuthTable userAuthTable) {
        this.userAuthTable = userAuthTable;
    }

    public UserAuthorizationDao getUserAuthDao() {
        return userAuthDao;
    }

    public void setUserAuthDao(UserAuthorizationDao userAuthDao) {
        this.userAuthDao = userAuthDao;
    }

    public RolesDao getRoleDao() {
        return roleDao;
    }

    public void setRoleDao(RolesDao roleDao) {
        this.roleDao = roleDao;
    }

    private JTextField filterField;
    @Override
    protected JComponent createControl() {
        
        JPanel filterPanel = new JPanel(new BorderLayout());
        JLabel filterLabel = getComponentFactory().createLabel("userAuthFilter.label");
        filterPanel.add(filterLabel, BorderLayout.WEST);

        String tip = getMessage("userAuthFilter.caption");
        filterField = getComponentFactory().createTextField();
        filterField.setToolTipText(tip);
        filterPanel.add(filterField, BorderLayout.CENTER);
        filterPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        //instancia tabela de relatórios
        userAuthTable = new UsersTableFactory().createUserTable();
        
        JPanel view = new JPanel(new BorderLayout());
        JScrollPane sp = getComponentFactory().createScrollPane(userAuthTable.getControl());
        view.add(filterPanel, BorderLayout.NORTH);
        view.add(sp, BorderLayout.CENTER);
        
        return view;
    }
    
    private class UsersTableFactory {

        public UserAuthTable createUserTable() {
            UserAuthTable userTable = new UserAuthTable(userAuthDao);

            userTable.setDoubleClickHandler(propExecutor);

            //Evento de filtro
            EventList baseList = userTable.getBaseEventList();
            TextFilterator filterator = GlazedLists.textFilterator(new String[]{"username"});
            FilterList filterList = new FilterList(baseList, new TextComponentMatcherEditor(filterField, filterator));
            userTable.setFinalEventList(filterList);

            //Configuração do menu de popup
            CommandGroup popup = new CommandGroup();
            popup.add((ActionCommand) getWindowCommandManager().getCommand("propertiesCommand", ActionCommand.class));
            popup.add((ActionCommand) getWindowCommandManager().getCommand("deleteCommand", ActionCommand.class));
            userTable.setPopupCommandGroup(popup);
            userTable.setStatusBar(getStatusBar());


            ValueModel selectionHolder = new ListSelectionValueModelAdapter(userTable.getSelectionModel());
            new ListSingleSelectionGuard(selectionHolder, propExecutor);
            new ListSingleSelectionGuard(selectionHolder, delExecutor);

            return userTable;
        }
    }
    
    private GuardedActionCommandExecutor propExecutor = new PropExecutor();
    private class PropExecutor extends AbstractActionCommandExecutor {

        public void execute() {
            new UserAuthPropertiesDialog(userAuthTable.getSelectedUser(), getUserAuthDao()).showDialog();
        }
    }
    
    private GuardedActionCommandExecutor delExecutor = new DelExecutor();
    private class DelExecutor extends AbstractActionCommandExecutor {

        public void execute() {
            // We know exactly one contact will be selected at this time because
            // of the guards put in place in prepareTable.
            final Users user = userAuthTable.getSelectedUser();
            // Query the user to be sure they want to do this
            String title = getMessage("UserAuthView.confirmDelete.title");
            String message = getMessage("UserAuthView.confirmDelete.message");
            ConfirmationDialog dlg = new ConfirmationDialog(title, message) {

                protected void onConfirm() {
                    // Delete the object from the persistent store.
                    getUserAuthDao().delete(user);
                    // And notify the rest of the application of the change
                    getApplicationContext().publishEvent(
                            new LifecycleApplicationEvent(LifecycleApplicationEvent.DELETED, user));
                }
            };
            dlg.showDialog();
        }
    }
    
    private ActionCommandExecutor newExecutor = new NewExecutor();
    private class NewExecutor implements ActionCommandExecutor {

        public void execute() {
            new UserAuthPropertiesDialog(getUserAuthDao()).showDialog();
        }
    }
    
    private ActionCommandExecutor refreshExecutor = new RefreshExecutor();

    private class RefreshExecutor implements ActionCommandExecutor {

        public void execute() {
            userAuthTable.refreshTable();
        }
    }
    
    protected void registerLocalCommandExecutors(PageComponentContext context) {
        context.register("newCommand", newExecutor);
        context.register(GlobalCommandIds.PROPERTIES, propExecutor);
        context.register(GlobalCommandIds.DELETE, delExecutor);
        context.register("refreshCommand", refreshExecutor);
    }
    

    public void onApplicationEvent(ApplicationEvent arg0) {
        userAuthTable.onApplicationEvent(arg0);
    }
}
